import requests

# 定义变量
base_url = 'http://localhost:8080'
login_url = f'{base_url}/login'
supplier_url = f'{base_url}/supplier'

# 登录表单数据
login_data = {
    'username': 'your_username',
    'password': 'your_password'
}

# 测试登录
def test_login():
    session = requests.Session()
    response = session.post(login_url, data=login_data)
    assert response.status_code == 302 # 期望状态码也可以是其他的，比如200
    assert response.headers['Location'] == base_url + '/'
    print('登录成功')

# 测试查询下拉框供应商信息
def test_getComboboxList():
    session = requests.Session()
    session.post(login_url, data=login_data) # 登录
    response = session.get(f'{supplier_url}/getComboboxList', params={'q': 'supplier name'})
    assert response.status_code == 200
    assert len(response.json()) > 0
    print('查询下拉框供应商信息测试通过')

# 测试查询供应商列表
def test_supplier_list():
    session = requests.Session()
    session.post(login_url, data=login_data) # 登录
    response = session.get(f'{supplier_url}/list', params={'page': 1, 'rows': 10, 'supplierName': 'supplier name'})
    assert response.status_code == 200
    assert len(response.json()['rows']) > 0
    print('查询供应商列表测试通过')

# 测试添加供应商
def test_add_supplier():
    session = requests.Session()
    session.post(login_url, data=login_data) # 登录
    supplier_data = {
        'supplierName': 'new supplier name',
        'contacts': 'new contacts',
        'phoneNumber': '12345678',
        'address': 'new address',
        'remarks': 'new remarks'
    }
    response = session.post(f'{supplier_url}/save', json=supplier_data)
    assert response.status_code == 200
    assert response.json()['status'] == 'success'
    print('添加供应商测试通过')

# 测试修改供应商
def test_update_supplier():
    session = requests.Session()
    session.post(login_url, data=login_data) # 登录
    supplier_id = 1 # 修改已存在的供应商
    supplier_data = {
        'supplierId': supplier_id,
        'supplierName': 'updated supplier name',
        'contacts': 'updated contacts',
        'phoneNumber': '12345678',
        'address': 'updated address',
        'remarks': 'updated remarks'
    }
    response = session.post(f'{supplier_url}/save', json=supplier_data)
    assert response.status_code == 200
    assert response.json()['status'] == 'success'
    # 确认修改是否生效
    updated_data_response = session.get(f'{supplier_url}/list', params={'page': 1, 'rows': 10, 'supplierName': 'updated supplier'})
    assert updated_data_response.status_code == 200
    assert len(updated_data_response.json()['rows']) > 0
    updated_data = updated_data_response.json()['rows'][0]
    assert updated_data['supplierId'] == supplier_id
    assert updated_data['supplierName'] == 'updated supplier name'
    assert updated_data['contacts'] == 'updated contacts'
    print('修改供应商测试通过')

# 测试删除供应商
def test_delete_supplier():
    session = requests.Session()
    session.post(login_url, data=login_data) # 登录
    # 查询并选择要删除的供应商ID
    response = session.get(f'{supplier_url}/list', params={'page': 1, 'rows': 10, 'supplierName': 'new supplier name'})
    assert response.status_code == 200
    assert len(response.json()['rows']) > 0 # 这里假设已经有一条名为"new supplier name"的记录
    supplier_id = response.json()['rows'][0]['supplierId']
    # 发送删除请求
    response = session.post(f'{supplier_url}/delete', params={'ids': supplier_id})
    assert response.status_code == 200
    assert response.json()['status'] == 'success'
    # 再次查询以确认是否已被删除
    deleted_data_response = session.get(f'{supplier_url}/list', params={'page': 1, 'rows': 10, 'supplierName': 'new supplier name'})
    assert deleted_data_response.status_code == 200
    assert len(deleted_data_response.json()['rows']) == 0
    print('删除供应商测试通过')


if __name__ == '__main__':
    test_login()
    test_getComboboxList()
    test_supplier_list()
    test_add_supplier()
    test_update_supplier()
    test_delete_supplier()

